#include "head.h"
void sigfunc(int signum)
{//Press "^\" to restart
    return ;
}
int main()
{
    signal(SIGQUIT,sigfunc);
	initialInterface();
	FILE* fp = fopen("configuration.txt", "r");
	if (NULL == fp)
	{
		perror("fopen");
	}
	char abcd[4][100] = { 0 };
	int i, j;//用于循环
	for (j = 1; j < 4; ++j)
	{
		fscanf(fp, "%s", abcd[j]);
	}
	//printf("%s", abcd[1]);
	FILE* fp1;
	FILE* fp2;
	//在对一个文件的操作完成后最好立马关闭它，
	//不然可能会出现一些意想不到的问题，等下次用到的时候再打开

	int choice;//用于选择功能
	int count;//用于计数
	int ret;//查找的返回值，用于判断是否有相关信息
	int flag;//用于注册用户时传递权限信息
	int len = 0;//统计记录条数
	puser pcuru;
	pcustomer pcurc;
	//proom pcurr;
	/********************客户**********************/
	pcustomer phead = NULL;//指向客户信息链表的头节点
	pcustomer ptail = NULL;//指向客户信息链表的尾节点
	int id;
	char name[20];
	int room_num;
	int confirm;//用于确认是否删除
	pcustomer pcur;//用于对指针数组赋值
	/********************用户**********************/
	puser puhead = NULL;
	puser putail = NULL;
	char uname[20];
	//char pass[20];
    char *pass;
	int right = -1;//记录用户权限及其他
	//len = recordNum(fp2);
	system_init(&puhead, &putail, &phead, &ptail);
	//len = recordNum(fp2);
	//fclose(fp1);
	//fclose(fp2);
	//fclose(fp3);

Start:
	system("clear");
	initialInterface();
	printf("**-------------------请选择：");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		count = 0;//用于初始化用户尝试登录次数
		goto Login;
		break;
	case 2:
		flag = 2;
		goto Register;
		break;
	case 3:
		return 0;
	default:
		printf("请重新输入！\n");
		pause();
		goto Start;
		break;
	}

Login:
	right = -1;
	system("clear");
	loginInterface();
	++count;
	while (-1 == right)
	{
		//loginInterface();
		printf("username:");
		scanf("%s", uname);
		//printf("password:");
		//input_password(pass);
        pass=getpass("password");
		//scanf("%s", pass);
		right = login(puhead, uname, pass);
		memset(pass, 0, strlen(pass));
		pause();
		if (-1 == right)
		{
			if (count > 5)
			{
				printf("-------Too many attempts!\n-------Please try again later!");
				sleep(100000);
				count = 0;
			}
			goto Login;
		}
		else if (1 == right)
		{
			goto Admin;
		}
		else if (2 == right)
		{
			goto User;
		}
	}


Admin:
	system("clear");
	homeInterface();
	printf("**-------------------请选择：");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		goto UserM;
		break;
	case 2:
		goto CustomerM;
		break;
	case 3:
		goto RoomM;
		break;
	case 4:
		goto Start;
	case 5:
		return 0;
	default:
		printf("请重新输入！\n");
		pause();
		goto Admin;
		break;
	}

User:
	system("clear");
	headInterface();
	printf("**-------------------客房信息：\n");
	printList(phead);
	pause();
	goto Start;

UserM:
	system("clear");
	userMInterface();
	printf("**-------------------请选择：");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		goto Showuser;
		break;
	case 2:
		goto Deleteuser;
		break;
	case 3:
		flag = 1;
		goto Register;
		break;
	case 4:
		goto Admin;
		break;
	case 5:
		goto Start;
		break;
	case 6:
		return 0;
	default:
		printf("请重新输入！\n");
		pause();
		goto UserM;
		break;
	}

CustomerM:
	system("clear");
	customerMInterface();
	printf("**-------------------请选择：");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		goto Showcustomer;
		break;
	case 2:
		goto Searchcustomer;
		break;
	case 3:
		goto Modifycustomer;
		break;
	case 4:
		goto Addcustomer;
		break;
	case 5:
		goto Deletecustomer;
		break;
	case 6:
		goto Sortcustomer;
		break;
	case 7:
		goto Admin;
		break;
	case 8:
		goto Start;
		break;
	case 9:
		return 0;
	default:
		printf("请重新输入！\n");
		pause();
		goto CustomerM;
		break;
	}

RoomM:
	system("clear");
	roomMInterface();
	printf("**-------------------请选择：");
	scanf("%d", &choice);
	switch (choice)
	{
	case 1:
		goto UserM;
		break;
	case 2:
		goto CustomerM;
		break;
	case 3:
		goto RoomM;
		break;
	default:
		printf("请重新输入！\n");
		pause();
		goto Admin;
		break;
	}

	/****************用户信息管理******************/
Showuser:
	printUser(puhead);
    pause();
	pause();
	goto UserM;
Register:
	/****************添加用户信息******************/
	pcuru = puhead;
	if (1 == flag)
	{
		printf("请输入用户名：");
		scanf("%s", uname);
		// printf("请输入密码：");
		// input_password(pass);
		pass = getpass("请输入密码：");
		printf("请输入用户权限：");
		scanf("%d", &right);
		ret = userInsert(&puhead, &putail, uname, pass, right);
		memset(pass, 0, strlen(pass));
		if (1 == ret)
		{
			fp1 = fopen(abcd[1], "w+");//文件指针文件指针*不能忘了加
			while (pcuru)
			{
				strcpy(uname, pcuru->uname);
				strcpy(pass, pcuru->pass);
				right = pcuru->right;
				fprintf(fp1, "%s %s %d\n", uname, pass, right);
				pcuru = pcuru->pnext;
			}
			printf("添加成功！\n");
			fclose(fp1);
		}
		else
		{
			goto UserM;
		}
	}
	else if (2 == flag)
	{
		printf("请输入用户名：");
		scanf("%s", uname);
		// printf("请输入密码：");
		// input_password(pass);
		pass = getpass("请输入密码：");
		right = 2;
		ret = userInsert(&puhead, &putail, uname, pass, right);
		memset(pass, 0, strlen(pass));
		if (1 == ret)
		{
			fp1 = fopen(abcd[1], "w+");//文件指针文件指针*不能忘了加
			while (pcuru)
			{
				strcpy(uname, pcuru->uname);
				strcpy(pass, pcuru->pass);
				right = pcuru->right;
				fprintf(fp1, "%s %s %d\n", uname, pass, right);
				pcuru = pcuru->pnext;
			}
			printf("添加成功！\n");
			fclose(fp1);
		}
		else
		{
			pause();
			goto UserM;
		}
	}
	pause();
	goto UserM;

Deleteuser:
	printf("请输入要删除的用户名：");
	scanf("%s", uname);
	ret = repeatCheckU(puhead, uname);
	if (1==ret)
	{
		printf("确定要删除吗？（1：是；0：否）：");
		scanf("%d", &confirm);
		if (1 == confirm)
		{
			deleteUser(&puhead, &putail, uname);
			printf("删除成功！\n");
			printUser(puhead);
		}
		else if (0 == confirm)
		{
			printf("好的，主人！\n");
		}
		else
		{
			printf("请输入0或1！\n");
		}
	}
	else
	{
		printf("没有该用户！\n");
	}


	fp1 = fopen(abcd[1], "w+");
	pcuru = puhead;
	while (pcuru)
	{
		strcpy(uname, pcuru->uname);
		strcpy(pass, pcuru->pass);
		right = pcuru->right;
		fprintf(fp1, "%s %s %d\n", uname, pass, right);
		pcuru = pcuru->pnext;
	}
	fclose(fp1);
	pause();
	goto UserM;


	/****************客户信息管理******************/
Showcustomer:
	/********************输出**********************/
	len = recordNum(abcd, 2);
	printf("\n======客户总数：%d\n", len);
	printList(phead);
	pause();
	goto CustomerM;
Searchcustomer:
	/********************查询**********************/
	printf("请输入要查询的客户id：");
	scanf("%d", &id);
	findId(phead, id);
	pause();
	goto CustomerM;
Modifycustomer:
	/********************修改**********************/
	printf("请输入要修改对应客房信息的客户id：");
	scanf("%d", &id);
	ret = findId(phead, id);
	if (ret)
	{
		printf("客房编号修改为：");
		scanf("%d", &room_num);
		modifyList(phead, id, room_num);
		findId(phead, id);
	}
	pcurc = phead;
	fp2 = fopen(abcd[2], "w+");
	while (pcurc)
	{
		id = pcurc->id;
		strcpy(name, pcurc->name);
		room_num = pcurc->room_num;
		pcurc = pcurc->pnext;
		fprintf(fp2, "%d %s %d\n", id, name, room_num);
	}
	fclose(fp2);
	pause();
	goto CustomerM;
Addcustomer:
	/****************添加客户信息******************/
	printf("请输入客户id：");
	scanf("%d", &id);
	printf("请输入客户姓名：");
	memset(name, 0, sizeof(name));
	scanf("%s", name);
	printf("请输入客户所在房间编号：");
	scanf("%d", &room_num);
	tailInsert(&phead, &ptail, id, name, room_num);
	pcurc = phead;
	fp2 = fopen(abcd[2], "w+");
	while (pcurc)
	{
		id = pcurc->id;
		strcpy(name, pcurc->name);
		room_num = pcurc->room_num;
		pcurc = pcurc->pnext;
		fprintf(fp2, "%d %s %d\n", id, name, room_num);
	}
	fclose(fp2);
	pause();
	goto CustomerM;

Deletecustomer:
	/********************删除**********************/
	printf("请输入要删除信息的客户id：");
	scanf("%d", &id);
	ret = findId(phead, id);
	if (ret)
	{
		printf("确定要删除吗？（1：是；0：否）：");
		scanf("%d", &confirm);
		if (1 == confirm)
		{
			deleteList(&phead, &ptail, id);
			printf("删除成功！\n");
			printList(phead);
		}
		else if (0 == confirm)
		{
			printf("好的，主人！\n");
		}
		else
		{
			printf("请输入0或1！\n");
		}
	}
	
	pcurc = phead;
	fp2 = fopen(abcd[2], "w+");
	while (pcurc)
	{
		id = pcurc->id;
		strcpy(name, pcurc->name);
		room_num = pcurc->room_num;
		pcurc = pcurc->pnext;
		fprintf(fp2, "%d %s %d\n", id, name, room_num);
	}
	//fflush(fp2);//把缓冲区中的内容立即写入文件
	fclose(fp2);

	pause();
	goto CustomerM;
Sortcustomer:
	//printf("%s", arg[2]);
	len = recordNum(abcd, 2);
	/****************排序客户信息******************/
	printf("After sort.\n");
	pcustomer* parr;
	parr = (pcustomer*)calloc(len, sizeof(pcustomer));
	pcur = phead;
	for (i = 0; i < len; ++i)
	{
		parr[i] = pcur;
		pcur = pcur->pnext;
	}
	qsort(parr, len, sizeof(pcustomer), comparePointer);
	printsorted(parr, len);
	i = 0;
	fp2 = fopen(abcd[2], "w+");
	//fseek(fp, 0, seek_set);
	while (i < len)
	{
		id = parr[i]->id;
		strcpy(name, parr[i]->name);
		room_num = parr[i]->room_num;
		fprintf(fp2, "%d %s %d\n", id, name, room_num);
		++i;
	}
	//fflush(fp2);//把缓冲区中的内容立即写入文件
	fclose(fp2);
	free(parr);
	pause();
	goto CustomerM;


	/****************客房信息管理******************/
	pause();

}
